package EBM.DB;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import EBM.model.User;

public class BugUserDAO {
	//private List<Object> response;
	private Connection conn;
	private PreparedStatement ps;
	private ResultSet rs;
	//private String sql;
	private int result;

	public BugUserDAO(){
		//response = null;
		conn = null;
		ps = null;
		rs = null;
		//sql = null;
		result = 0;
	}

	public User findByBugid(Object proid, Object bugid, Object user_type)
	throws SQLException, ClassNotFoundException{
		
		String sql="select * from bug_user_"+ proid + ",userinfo"+
		" where bug_user_"+proid + ".userid=userinfo.id"+
		" and bug_user_"+proid + ".bugid=?"+
		" and bug_user_"+proid + ".user_type=?";

		DBConnection dbconn = new DBConnection();
		conn = dbconn.getConnNew();
		ps = conn.prepareStatement(sql);
		ps.setObject(1, bugid);
		ps.setObject(2, user_type);

		//System.out.println("语句："+sql);
		
		//response = new ArrayList<Object>();

		rs = ps.executeQuery();
		User user = new User();
		if(rs.next()){
			user.setId(rs.getString("userinfo.id"));
			user.setUsrid(rs.getString("userinfo.usrid"));
			user.setName(rs.getString("userinfo.name"));
		}
		conn.close();

		return user;
	}

	public boolean save(Object proid, Object bugid, Object userid, Object user_type)
	throws SQLException, ClassNotFoundException{

		String sql="insert into bug_user_"+ proid +
		"(bugid,userid,user_type)"+
		" values(?,?,?)";

		DBConnection dbconn = new DBConnection();
		conn = dbconn.getConnNew();
		ps = conn.prepareStatement(sql);
		ps.setObject(1, bugid);
		ps.setObject(2, userid);
		ps.setObject(3, user_type);

		//response = new ArrayList<Object>();

		result = ps.executeUpdate();
		conn.close();

		if(result>0){
			return true;
		}else{
			return false;
		}
	}

	public boolean delete(Object proid, Object bugid, Object user_type)
	throws SQLException, ClassNotFoundException{

		String sql="delete from bug_user_"+ proid +" where bugid=? and user_type=?";

		DBConnection dbconn = new DBConnection();
		conn = dbconn.getConnNew();
		ps = conn.prepareStatement(sql);
		ps.setObject(1, bugid);
		ps.setObject(2, user_type);

		//response = new ArrayList<Object>();

		try{
			ps.executeUpdate();
		}catch(SQLException e){
			return false;
		}finally{
			conn.close();
		}
		
		return true;
	}

	public boolean update(Object proid, Object bugid, Object userid, Object user_type)
	throws SQLException, ClassNotFoundException{

		String sql="update bug_user_"+ proid +" set userid=? where bugid=? and user_type=?";

		DBConnection dbconn = new DBConnection();
		conn = dbconn.getConnNew();
		ps = conn.prepareStatement(sql);
		ps.setObject(1, userid);
		ps.setObject(2, bugid);
		ps.setObject(3, user_type);

		//response = new ArrayList<Object>();

		result = ps.executeUpdate();
		conn.close();

		if(result>0){
			return true;
		}else{
			return false;
		}
	}
}
